package dev.ece.suit.ui.shader;

import java.io.FileNotFoundException;
import java.io.InputStream;
import java.util.Map;

import org.lwjgl.opengl.GL20;

import dev.ece.core.shader.DefaultTextureShader;
import dev.ece.util.lib.Vector2f;
import dev.ece.util.lib.Vector4f;

public class ButtonTextureShader extends DefaultTextureShader {

	private static ButtonTextureShader defaultButtonTextureShader;
	
	public static ButtonTextureShader getDefaultButtonTextureShader() throws FileNotFoundException {
		if(defaultButtonTextureShader == null) {
			defaultButtonTextureShader = new ButtonTextureShader(
					ButtonTextureShader.class.getClassLoader().getResourceAsStream("shaders/ui/default.vert"), 
					ButtonTextureShader.class.getClassLoader().getResourceAsStream("shaders/ui/defaultbutton.frag"));
		}
		return defaultButtonTextureShader;
	}
	
	private int disabledHandler;
	
	private int selectedHandler;
	
	private int selectedColorHandler;
	
	private int pressedHandler;
	
	private int pressedOffsetHandler;
	
	public ButtonTextureShader(String vertexShaderSource, String pixelShaderSource) {
		super(vertexShaderSource, pixelShaderSource);
	}

	public ButtonTextureShader(InputStream vertexShaderInputStream, InputStream pixelShaderInputStream,
			Map<String, String> functions) {
		super(vertexShaderInputStream, pixelShaderInputStream, functions);
	}

	public ButtonTextureShader(InputStream vertexShaderInputStream, InputStream pixelShaderInputStream) {
		super(vertexShaderInputStream, pixelShaderInputStream);
	}

	public ButtonTextureShader(StringBuffer vertexShaderSource, StringBuffer pixelShaderSource,
			Map<String, String> functions) {
		super(vertexShaderSource, pixelShaderSource, functions);
	}
	
	@Override
	protected void onInitializeHandler(int program) {
		super.onInitializeHandler(program);
		disabledHandler = GL20.glGetUniformLocation(program, "disabled");
		selectedHandler = GL20.glGetUniformLocation(program, "selected");
		selectedColorHandler = GL20.glGetUniformLocation(program, "selectedColor");
		pressedHandler = GL20.glGetUniformLocation(program, "pressed");
		pressedOffsetHandler = GL20.glGetUniformLocation(program, "pressedOffset");
	}
	
	public void glDisabled(boolean disabled) {
		GL20.glUniform1i(disabledHandler, disabled ? 1 : 0);
	}
	
	public void glPressed(boolean pressed, Vector2f pressedOffset) {
		if(pressed) {
			GL20.glUniform1i(pressedHandler, 1);
			GL20.glUniform2f(pressedOffsetHandler, pressedOffset.getX(), pressedOffset.getY());
		} else {
			GL20.glUniform1i(pressedHandler, 0);
		}
	}
	
	public void glSelected(boolean selected, Vector4f selectedColor) {
		if(selected) {
			GL20.glUniform1i(selectedHandler, 1);
			GL20.glUniform4f(selectedColorHandler, 
					selectedColor.getX(), selectedColor.getY(), selectedColor.getZ(), selectedColor.getW());
		} else {
			GL20.glUniform1i(selectedHandler, 0);
		}
	}

	@Override
	public void clearup() {
		GL20.glDeleteShader(disabledHandler);
		GL20.glDeleteShader(selectedHandler);
		GL20.glDeleteShader(selectedColorHandler);
		GL20.glDeleteShader(pressedHandler);
		GL20.glDeleteShader(pressedOffsetHandler);
		super.clearup();
	}

}
